<?php
declare(strict_types=1);
/**
 * Created by PhpStorm
 * User: Administrator
 * Date: 2023/8/4
 * Time: 14:38:44
 */

namespace Service\CommodityService;

use App\Model\BbcModel\Country;
use Service\Code\Normal;
use Service\BaseService;

/**
 * 国家管理
 */
class CountryService extends BaseService
{
    public function makeWhere($param)
    {
        $where = [
            ['sid','=',$param['sid']],
            ['df','=',0]
        ];

        if(isset($param['word']) && $param['word'] != ''){
            $where[] = ['name|short_name','like',"%{$param['word']}%"];
        }

        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['status','=',$param['status']];
        }


        return $where;
    }
    public function countryList($param){
        $page = $param['page'] ?? 1;
        $limit = $param['limit'] ?? 10;
        $where = $this->makeWhere($param);
        $data_list = Country::instance()
            ->where($where)
            ->paginate($limit, ['id','name','short_name','country_logo','icon','is_show'], 'page', $page);

        $total = $data_list->total();
        $list = $data_list->items();
        foreach ($list as &$v){
            $v['is_show_text'] = $v['is_show'] == 1 ? '启用':'禁用';
        }

        $brand_status_list = Country::instance()
            ->where($where)
            ->pluck('is_show')->toArray();

        $brand_status_map = array_count_values($brand_status_list);
        $enable_num = $brand_status_map[1] ?? 0;
        $disable_num = $brand_status_map[0] ?? 0;

        return Normal::returnCode(Normal::SUC,'请求成功',['list'=>$list,'total'=>$total,'enable_num'=>$enable_num,'disable_num'=>$disable_num]);
    }

    public function details($param){
        if(empty($param['id'])){
            return Normal::returnCode(Normal::FAIL,'缺少国家ID');
        }
        $info = Country::instance()
            ->where(['id'=>$param['id'],'df'=>0])
            ->select(['id','name','short_name','icon','country_logo'])
            ->first();
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'国家数据不存在');
        }

        return Normal::returnCode(Normal::SUC,'请求成功',$info);
    }

    public function edit($param){
        if(empty($param['id'])){
            return Normal::returnCode(Normal::FAIL,'缺少国家ID');
        }

        if(empty($param['name'])){
            return Normal::returnCode(Normal::FAIL,'名称不能为空');
        }

        $info = Country::instance()
            ->where(['id'=>$param['id'],'df'=>0])
            ->first();
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'国家数据不存在');
        }

        $res = Country::instance()
            ->where(['id'=>$param['id']])
            ->update([
                'name' => $param['name'],
                'short_name' => $info['short_name'] ?? '',
                'icon' => $info['icon'] ?? '',
                'country_logo' => $info['country_logo'] ?? '',
                'md' => time(),
            ]);

        if(!$res){
            return Normal::returnCode(Normal::FAIL,'编辑失败');
        }

        return Normal::returnCode(Normal::SUC,'编辑成功');

    }

    public function changeStatus($param) : array{
        if(!isset($param['country_ids']) || !$param['country_ids']){
            return Normal::returnCode(Normal::FAIL,'请选择国家');
        }
        if(!isset($param['status'])){
            return Normal::returnCode(Normal::FAIL,'缺少更改的状态参数');
        }

        $status = $param['status'];
        $res = Country::instance()
            ->where(['cid'=>$param['cid'],'sid'=>$param['sid']])
            ->whereIn('id',$param['country_ids'])
            ->update(['status'=>$status,'md'=>time()]);

        if(!$res){
            return Normal::returnCode(Normal::FAIL,'操作失败');
        }
        return Normal::returnCode(Normal::SUC,'操作成功');
    }
}